package com.netintech.project.system.videoPacketLossRate.controller;

import com.netintech.common.exception.lossRateCheck.isCheckingException;
import com.netintech.core.web.controller.BaseController;
import com.netintech.core.web.domain.RequestResult;
import com.netintech.project.system.ffmpeg.bo.FfmpegProperties;
import com.netintech.project.system.videoPacketLossRate.service.LossRateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("lossRate")
@EnableConfigurationProperties(FfmpegProperties.class)
public class LossRateController extends BaseController {

    @Autowired
    private LossRateService lossRateService;

    @Autowired
    private FfmpegProperties ffmpegProperties;


    @RequestMapping("execute")
    public RequestResult list(@RequestParam(defaultValue = "10000") Integer seconds, @RequestParam(defaultValue = "tcp") String transportType) {
        try {
            return RequestResult.success(lossRateService.execute(seconds, transportType, ffmpegProperties.getRtspUrl()));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return RequestResult.error(e.getMessage());
        }

    }

    @RequestMapping("executeFromExcel")
    public RequestResult executeFromExcel(@RequestParam(defaultValue = "30000") Integer seconds,
                                          @RequestParam(defaultValue = "tcp") String transportType) {
        Exception exception=null;
        try {
            lossRateService.executeFromExcel(seconds, transportType);
            return RequestResult.success(null);
        } catch (Exception e) {
            exception=e;
            e.printStackTrace();
            log.error(e.getMessage());
            return RequestResult.error(e.getMessage());
        } finally {
            lossRateService.excelInit(exception);
        }

    }

    @RequestMapping("executeFromDb")
    public RequestResult executeFromDb(@RequestParam(defaultValue = "30000") Integer seconds,
                                       @RequestParam(defaultValue = "tcp") String transportType) {
        Exception exception = null;
        try {
            lossRateService.executeFromDb(seconds, transportType);
            return RequestResult.success(null);
        } catch (Exception e) {
            exception = e;
            e.printStackTrace();
            log.error(e.getMessage());
            return RequestResult.error(e.getMessage());
        } finally {
            lossRateService.dbInit(exception);
        }

    }


    @RequestMapping("testExcel")
    public RequestResult testExcel() {
        HSSFWorkbook hssfWorkbook = null;
        try {
            hssfWorkbook = new HSSFWorkbook(new FileInputStream(new File(ffmpegProperties.getExcelOutPutFilePath())));
            HSSFSheet sheet1 = hssfWorkbook.getSheet("Sheet1");
            return RequestResult.success(sheet1.getRow(1).getCell(1).getStringCellValue());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return RequestResult.error(null);

    }


}
